<template>
  <a-card class="body-container">
    <s-list-form>
      <template #less>
        <s-form-item size="list-middle" label="分组名称">
          <s-input v-model="query.groupName" placeholder="分组名称" allow-clear />
        </s-form-item>
        <s-form-item size="list-middle" label="分组编号">
          <s-input v-model="query.groupId" placeholder="分组编号" allow-clear />
        </s-form-item>
      </template>
      <template #more />
    </s-list-form>
    <s-tool-bar>
      <template #left>
        <s-button
          v-if="authorize.group.insert"
          icon="plus-outlined"
          type="primary"
          @click="
            $refs.add.init((dialog) => {
              dialog.resetFormValue(), dialog.open()
            })
          "
        >
          新增
        </s-button>
        <a-popconfirm
          v-if="authorize.group.deleteMulti"
          title="确认删除所选数据？"
          ok-text="是"
          cancel-text="否"
          @confirm="deleteMulti"
        >
          <s-button type="danger" icon="delete-outlined"> 删除 </s-button>
        </a-popconfirm>
      </template>
      <template #right>
        <s-button type="primary" icon="search-outlined" @click="$refs.table.search(query)">
          查询
        </s-button>
        <s-button style="margin-left: 8px" icon="stop-outlined" @click="query = {}">
          重置
        </s-button>
      </template>
    </s-tool-bar>
    <s-table
      ref="table"
      bordered
      :scroll="{ y: tableHeight }"
      :columns="columns"
      url="/data/dict/group/pageList"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <a
            v-if="authorize.group.selectOneById"
            style="margin-right: 8px"
            @click=";(selectRowId = record.id), $refs.detail.init()"
          >
            <s-icon type="eye-outlined" />查看
          </a>
          <a
            v-if="authorize.group.update"
            style="margin-right: 8px"
            @click=";(selectRowId = record.id), $refs.update.init()"
          >
            <s-icon type="edit-outlined" />编辑
          </a>
          <a
            v-if="authorize.item.pageList"
            style="margin-right: 8px"
            @click="$refs.dictItem.open(record)"
          >
            <s-icon type="edit-outlined" />字典配置
          </a>
          <a-popconfirm
            v-if="authorize.group.delete"
            title="确认删除此条数据？"
            ok-text="是"
            cancel-text="否"
            @confirm="deleteById(record.id)"
          >
            <a style="margin-right: 8px"> <s-icon type="delete-outlined" />删除 </a>
          </a-popconfirm>
        </template>
      </template>
    </s-table>
    <!--    <detail ref="detail" />
    <add
      ref="add"
      @success="
        () => {
          $refs.table.refresh()
        }
      "
    />
    <update
      ref="update"
      @success="
        () => {
          $refs.table.refresh()
        }
      "
    />-->
    <s-dynamic-form-dialog
      ref="add"
      title="添加"
      form-id="4f98ee6371c04a8f96074e33a8a3b1c6"
      @save="addItem"
    ></s-dynamic-form-dialog>
    <s-dynamic-form-dialog
      ref="detail"
      title="详情"
      disabled
      :pull-form-data="
        () => {
          return $api.GET('/data/dict/group/selectOneById', { id: selectRowId })
        }
      "
      form-id="4f98ee6371c04a8f96074e33a8a3b1c6"
    >
      <template #default="{ form }">
        <s-form-item label="创建人">
          <s-input v-model="form.createByAccount" disabled />
        </s-form-item>
        <s-form-item label="创建时间">
          <s-input v-model="form.createDate" disabled />
        </s-form-item>
        <s-form-item label="修改人">
          <s-input v-model="form.updateByAccount" disabled />
        </s-form-item>
        <s-form-item label="修改时间">
          <s-input v-model="form.updateDate" disabled />
        </s-form-item>
      </template>
    </s-dynamic-form-dialog>
    <s-dynamic-form-dialog
      ref="update"
      title="编辑"
      form-id="4f98ee6371c04a8f96074e33a8a3b1c6"
      :pull-form-data="
        () => {
          return $api.GET('/data/dict/group/selectOneById', { id: selectRowId })
        }
      "
      @save="updateItem"
    >
    </s-dynamic-form-dialog>
    <dict-item ref="dictItem" />
  </a-card>
</template>

<script>
const columns = [
  {
    title: '分组名称',
    dataIndex: 'groupName',
    ellipsis: true,
    width: 200
  },
  {
    title: '分组编号',
    dataIndex: 'groupId',
    sorter: true,
    ellipsis: true,
    width: 200
  },
  {
    title: '创建人',
    dataIndex: 'createByAccount',
    ellipsis: true,
    width: 200
  },
  {
    title: '创建时间',
    dataIndex: 'createDate',
    ellipsis: true,
    width: 200
  },
  {
    title: '修改人',
    dataIndex: 'updateByAccount',
    ellipsis: true,
    width: 200
  },
  {
    title: '修改时间',
    dataIndex: 'updateDate',
    ellipsis: true,
    width: 200
  },
  {
    title: '操作',
    fixed: 'right',
    key: 'action',
    width: 300
  }
]
// import Detail from './detail/index.vue'
// import Add from './add/index.vue'
// import Update from './update/index.vue'
import DictItem from '../item/index.vue'
export default {
  name: 'QueryList',
  components: { DictItem },
  data() {
    return {
      query: {},
      columns: columns,
      selectRowId: null
    }
  },
  authorize: {
    group: '/data/dict/group/permissionList',
    item: '/data/dict/item/permissionList'
  },
  methods: {
    addItem(values, fullValue, dialog) {
      this.$api
        .PUT('/data/dict/group/insert', values)
        .then((res) => {
          dialog.close()
          this.$refs.table.refresh()
          this.$message.info(res.msg)
        })
        .catch(() => {
          dialog.reset()
        })
    },
    updateItem(values, fullValue, dialog) {
      values.id = fullValue.id
      this.$api
        .PUT('/data/dict/group/update', values)
        .then((res) => {
          dialog.close()
          this.$refs.table.refresh()
          this.$message.info(res.msg)
        })
        .catch(() => {
          dialog.reset()
        })
    },
    deleteMulti() {
      const keys = this.$refs.table.getSelectedKeys()
      if (keys.length > 0) {
        this.$loading(true)
        this.$api
          .DELETE('/data/dict/group/deleteMulti', { ids: keys.toString() })
          .then(() => {
            this.$loading(false)
            this.$refs.table.search(this.query)
          })
          .catch(() => {
            this.$loading(false)
          })
      }
    },
    deleteById(id) {
      this.$loading(true)
      this.$api
        .DELETE('/data/dict/group/delete', { id: id })
        .then(() => {
          this.$loading(false)
          this.$refs.table.search(this.query)
        })
        .catch(() => {
          this.$loading(false)
        })
    }
  }
}
</script>
